<?php
/**
 * @Author: ant
 * @time:2018-1-19 
 * @Effect: 文件处理 
 */

namespace app\admin\controller;


class FileMana  extends Common
{
	/*
	 *图片上传本地 
	 */
	private  $PageTitle;
	public function local_upload_img()
    {
        $this->PageTitle='上传图片';
        $this->generalInfor($this->PageTitle);
        return $this->fetch();
    }
    //七牛图片上传
    public function qiniu_upload_img()
    {
       
        return $this->fetch();
    }
    //上传到本地
    public function localuploadifyImg()
    {

    // 获取表单上传文件 例如上传了001.jpg
        //halt($_FILES);
        $path ="/uploads/";
        $file = request()->file('images');
        //halt($file);
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        $path = $path . $info->getSaveName();
        
            // 成功上传后 返回上传信息
        return json_encode(array('state'=>1,'path'=>$path));
        }else{
            // 上传失败返回错误信息
        return json(array('state'=>0,'errmsg'=>'上传失败'));
        }
    } 
    //上传到七牛
    public function uploadifyImg(){  
       
         $image = Upload::image($_FILES,$_POST['imgtype']); 
         //图片样式处理
         $fops = 'imageView2/1/w/800/h/100';
        // 捕获异常  
        try{  
            // 返回qiniu上的文件名  
           
        }catch(\Exception $e){  
            echo json_encode(['status'=>0,'message'=>$e->getMessage()]);  
        }  
        // 返回给uploadify插件状态  
        if($image){  
            $data = [  
                'status' => 1,  
                'message' => 'OK',  
                'path' => config('qiniu.image_url').'/'.$image.'?'.$fops,  
            ];  
            echo json_encode($data);exit;       
        }else{  
            echo json_encode(['status'=>0,'message'=>'上传失败']);  
        }  
    }  
    /**
     * 上传excel表格
     */
    public function local_excel()
    {
    	return $this->fetch();
    }
    public function uploadUserOrder()
    {  
      if(Request::instance()->isPost()){  
        $file = request()->file('file');        // 获取表单提交过来的文件  
        $error = $_FILES['file']['error'];  // 如果$_FILES['file']['error']>0,表示文件上传失败  
        if(!$error){  
          $dir = ROOT_PATH . 'public' . DS . 'upload';  
          // 验证文件并移动到框架应用根目录/public/uploads/ 目录下  
          $info = $file->validate(['size'=>3145728,'ext'=>'xls,xlsx,csv'])->rule('uniqid')->move($dir);  
          /*判断是否符合验证*/  
          if($info){    //  符合类型  
            $extension = $info->getExtension();  

            $filename = $dir. DS .$info->getSaveName(); 

            //echo $filename;  
            Vendor("PHPExcel.IOFactory");
                if ($extension =='xlsx') {
                    $reader = \PHPExcel_IOFactory::createReader("Excel2007");
                    $PHPExcel = $reader ->load($filename);
                } else if ($extension =='xls') {
                    $reader = \PHPExcel_IOFactory::createReader("Excel5");
                    $PHPExcel = $reader ->load($filename);
                } else if ($extension=='csv') {
                    $reader = \PHPExcel_IOFactory::createReader("CSV");

                    //默认输入字符集
                    $reader->setInputEncoding('GBK');

                    //默认的分隔符
                    $reader->setDelimiter(',');

                    //载入文件
                    $PHPExcel = $reader->load($filename);
                }
/*
            $reader = \PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel2007格式  

            $PHPExcel = $reader->load($filename); // 载入excel文件  
            halt($PHPExcel);*/
            $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表  
            $highestRow = $sheet->getHighestRow(); // 取得总行数  
            $highestColumm = $sheet->getHighestColumn(); // 取得总列数 
            //判断上传是否为订单数据
            $SecondField="买家会员名";
            $number=1;
            $is_order=$sheet->getCell('B'.$number)->getValue(); 
            if($is_order!=$SecondField)
            {
                echo "不是订单数据";
                exit;    
            } 
            /** 循环读取每个单元格的数据 */  
            //$User = new User; 
            //halt($sheet); 
            //$res=db('tborder')->where('tb_refundm','0.00')->delete();
            //halt($res);
            //原来的数据条数
            //$oldList=db('tborder')->count();
            $where=array();
             $i=0;
            for ($row = 2; $row <= $highestRow; $row++)
            {//行数是以第1行开始，这里示例中excel有3列字段 
              $orderId = $sheet->getCell('A'.$row)->getValue();
              $actualPay = $sheet->getCell('I'.$row)->getValue(); 
              $BuyerName = $sheet->getCell('B'.$row)->getValue();  
              $phone = $sheet->getCell('Q'.$row)->getValue(); 
              $BuyerMessage= $sheet->getCell('L'.$row)->getValue();
              $address= $sheet->getCell('N'.$row)->getValue();
              $orderPayTime= $sheet->getCell('S'.$row)->getValue();
              $refundM= $sheet->getCell('AS'.$row)->getValue();

              $orderId=ltrim($orderId, '="');
              $orderId=rtrim($orderId, '"'); 
            /*  if(empty($orderPayTime))
              {
                    $orderPayTime=0;
              }*/
              $phone=ltrim($phone, "'");
             // $yanorderid=""$orderId".";
              //$orderIdone=(string)$orderId;
              //halt($orderIdone);
              $where[$row]['tb_orderid']=$orderId;
              $where[$row]['tb_actualpay']=$actualPay;
              $where[$row]['tb_Buyername']=$BuyerName;
              $where[$row]['tb_Buyermessage']=$BuyerMessage;
              $where[$row]['tb_address']=$address;
              $where[$row]['tb_orderpaytime'] = $orderPayTime;
              $where[$row]['tb_phone'] = $phone;
              $where[$row]['tb_refundm'] = $refundM;
              $where[$row]['createtime'] = time();
            
           
                 /* if($where && $orderPayTime!="")
                    {
                        $res=db('tborder')->where('tb_orderid',$orderId)->field('tb_id')->find();
                        //halt($res);
                        if(!isset($res['tb_id']))
                        {
                            $res=db('tborder')->insert($where[$row]);
                        }
                    }*/
                    //$User->data($data,true)->isUpdate(false)->save();  
                }
             
            }
            /*//添加后的的数据条数
            $NewsList=db('tborder')->count();
            $List=$NewsList-$oldList;
            echo '数据加载完成,共更新了'.$List.'条'; */
            $this->success('导入数据库成功',url('index'));  
          } else{ //  不符合类型业务  
            $this->error('请选择上传3MB内的excel表格文件...');  
            //echo $file->getError();  
          }  
        }else{  
          $this->error('请选择需要上传的文件...');  
        }  
    }

}